//
//  ViewController.m
//  NSInputStream_NSOutputStream
//
//  Created by Xie Wei on 2019/7/12.
//  Copyright © 2019 Xie Wei. All rights reserved.
//

#import "ViewController.h"

@interface ViewController () <NSStreamDelegate> {
    NSInteger _count;
}

@property (nonatomic, strong) NSMutableData *data;
@property (nonatomic, strong) NSInputStream *inputStream;

// 沙盒路径
@property (nonatomic, strong) NSString *fullPath;
// 输出流
@property (nonatomic, strong) NSOutputStream *outPutStream;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    _count = 0;
    
    [self testNSInputStream];
}

- (void)testNSInputStream {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp4"];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    self.inputStream = [NSInputStream inputStreamWithData:data];
    // inputStreamWithFileAtPath: 这个方法使用时有问题
//    self.inputStream = [NSInputStream inputStreamWithFileAtPath:filePath];
    self.inputStream.delegate = self;
    [self.inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    [self.inputStream open];
    
    // 写数据到沙盒中
    self.fullPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"test.mp4"];
    NSLog(@"%@", self.fullPath);
    self.outPutStream = [[NSOutputStream alloc] initToFileAtPath:self.fullPath append:YES];
    // 打开输入流
    [self.outPutStream open];
}

/**
 NSStreamEventOpenCompleted：文件打开成功
 NSStreamEventHasBytesAvailable：读取到数据
 NSStreamEventEndEncountered：文件读取结束
 */
- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode {
    switch(eventCode) {
        case NSStreamEventOpenCompleted:
            NSLog(@"文件打开,准备读取数据");
            break;
        case NSStreamEventHasBytesAvailable: {
            uint8_t buf[1024];
            NSInteger readLength = [self.inputStream read:buf maxLength:1024];
            if (readLength > 0) {
                if (_count == 0) {
                    NSLog(@"数据读取中...");
                }
                _count++;
                
                [self.data appendBytes:(const void *)buf length:readLength];
            } else {
                NSLog(@"未读取到数据");
            }
            break;
        }
        case NSStreamEventEndEncountered: {
            NSLog(@"数据读取结束");
            
            // 写数据
            [self.outPutStream write:self.data.bytes maxLength:self.data.length];
            
            [aStream close];
            [aStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
            aStream = nil;
            
            [self.outPutStream close];
            self.outPutStream = nil;
            break;
        }
        default:
            break;
    }
}

- (NSMutableData *)data {
    if (_data == nil) {
        _data = [NSMutableData data];
    }
    return _data;
}


@end
